#!/bin/bash

if [[ ! -e /tmp/checkcsdev ]];then
  echo "File does not exist"
  touch /tmp/checkcsdev
  echo 1 > /tmp/checkcsdev
fi

podnumcs=`kubectl get pod -n cs-dev|grep aas |wc -l`
podnumoms4=`kubectl get pod -n cs-oms4|grep aas |wc -l`
if [[ $podnumcs -eq 0 && `cat /tmp/checkcsdev` -eq 1 ]];then
  echo "stop cs-dev env and start cs-oms4 env"
  for i in `kubectl get deployments -n cs-dev|grep -v api-doc|awk '{print $1}'|awk 'NR>1'`
  do
     kubectl scale deployment $i --replicas=0 -n cs-dev
  done
  for j in `kubectl get deployments -n cs-oms4 |awk '{print $1}'|awk 'NR>1'`
  do
     kubectl scale deployment $j --replicas=1 -n cs-oms4
  done
  echo 0 > /tmp/checkcsdev
elif [[ $podnumoms4 -eq 0 && `cat /tmp/checkcsdev` -eq 0 ]];then
  echo "stop cs-oms4 env and start cs-dev env"
  for i in `kubectl get deployments -n cs-dev|grep -v api-doc|awk '{print $1}'|awk 'NR>1'`
  do
     kubectl scale deployment $i --replicas=1 -n cs-dev
  done
  for j in `kubectl get deployments -n cs-oms4|awk '{print $1}'|awk 'NR>1'`
  do
     kubectl scale deployment $j --replicas=0 -n cs-oms4
  done
  echo 1 > /tmp/checkcsdev
fi
